package cn.szzsi.business.controller.admin;

import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.common.util.EHcacheUtil;
import cn.szzsi.model.Resource;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.ehcache.EvictInterceptor;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 商户后台类 - 文章分类
 *
 */
public class ResourceController extends BaseAdminController<Resource> {
    private Resource businessResource;
    private String shopId;

    // 列表
    public void list(){
        shopId=getAttr("shopId");
        List<Resource>resourceTreeList= CacheKit.get("resourceTreeList",shopId);
        if(resourceTreeList==null){
            resourceTreeList=Resource.dao.getTreeByUid(shopId);
            CacheKit.put("resourceTreeList",shopId,resourceTreeList);
        }
        setAttr("resourceTreeList", resourceTreeList);
        render(CommonUtil.TEMPLATE_PATH+"/programmer/resource/business_resource_list.html");
    }
    // 添加
    public void add() {
        shopId=getAttr("shopId");
        setAttr("resourceTreeList", Resource.dao.getTreeByUid(shopId));
        render(CommonUtil.TEMPLATE_PATH+"/programmer/resource/business_resource_input.html");
    }

    // 编辑
    public void edit() {
        shopId=getAttr("shopId");
        String id = getPara("id","");
        if(StrKit.notBlank(id)){
            setAttr("resource", Resource.dao.findById(id));
        }
        setAttr("articleCategoryTreeList", Resource.dao.getByUid(shopId));
        render(CommonUtil.TEMPLATE_PATH+"/programmer/resource/business_resource_input.html");
    }

    // 添加
    @Before(EvictInterceptor.class)
    @CacheName("resourceList")
    public void save(){
        shopId=getAttr("shopId");
        businessResource = getModel(Resource.class);
        businessResource.set("uid",shopId);
        businessResource.set("id", CommonUtil.getUUID());
        Resource parent = Resource.dao.findById(businessResource.getStr("parent_id"));
        if (parent != null) {
            String parentPath = parent.getStr("path");
            businessResource.set("path",parentPath + Resource.PATH_SEPARATOR + businessResource.getStr("id"));
        } else {
            businessResource.set("path",businessResource.getStr("id"));
        }
        businessResource.set("level", StringUtils.countMatches(businessResource.getStr("path"), Resource.PATH_SEPARATOR)+1);
        businessResource.set("createDate", new Date());
        businessResource.save();
        renderJson(Ret.ok("info","添加成功").set("url","list"));

    }
    // 修改
    @Before(EvictInterceptor.class)
    @CacheName("resourceList")
    public void update(){
        businessResource = getModel(Resource.class);
        System.out.println(JsonKit.toJson(businessResource));
        businessResource.set("level", StringUtils.countMatches(businessResource.getStr("path"), Resource.PATH_SEPARATOR)+1);
        updated(businessResource);
        renderJson(Ret.ok("info","修改成功").set("url","list"));
    }

    // 删除
    @Before(EvictInterceptor.class)
    @CacheName("resourceList")
    public void delete(){
        String id = getPara("id","");
        Resource businessResource = Resource.dao.findById(id);
        List<Resource> childrenArticleCategoryList = businessResource.getChildren();
        if (childrenArticleCategoryList != null && childrenArticleCategoryList.size() > 0) {
            ajaxJsonErrorMessage("此文章分类存在下级分类，删除失败!");
        }
        Resource.dao.deleteById(id);

        renderJson(Ret.ok());
    }

    @Clear
    public void clerCache(){
        CacheKit.removeAll(EHcacheUtil.RESOURCE_MENU);
        renderJson(Ret.ok());
    }


    @Clear
    public void getRouteByUrl(){
        JSONObject jsonObject=new JSONObject();
        String url=getPara("url");
        Resource resource=Resource.dao.getByUrl(url);
        if(resource!=null){
            jsonObject.put("code",0);
            jsonObject.put("msg","");
            jsonObject.put("route",resource.getId());
        }else{
            jsonObject.put("code",1);
            jsonObject.put("msg","路由不存在");
        }
        renderJson(jsonObject);
    }

    public static void main(String[] args) {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("route","1212121211");
        System.out.println(jsonObject);
    }

}
